const DynamicProgramming = require("../template")

function testOptimizationProblems() {
  const dp = new DynamicProgramming()

  console.log(
    "1. 0-1背包问题基础测试:",
    (() => {
      const weights = [1, 3, 4, 5]
      const values = [1, 4, 5, 7]
      return dp.knapsack([...weights], [...values], 7) === 9
    })()
  )

  console.log(
    "2. 0-1背包问题复杂测试:",
    (() => {
      const weights = [2, 3, 4, 5]
      const values = [3, 4, 5, 6]
      return dp.knapsack([...weights], [...values], 5) === 7
    })()
  )

  console.log(
    "3. 0-1背包问题边界测试:",
    (() => {
      const weights = [1, 2, 3]
      const values = [6, 10, 12]
      return dp.knapsack([...weights], [...values], 5) === 22
    })()
  )

  console.log(
    "4. 硬币找零标准情况测试:",
    (() => {
      const coins = [1, 2, 5]
      return dp.coinChange([...coins], 11) === 3 &&
             dp.coinChange([...coins], 6) === 2
    })()
  )

  console.log(
    "5. 硬币找零无解情况测试:",
    (() => {
      const coins = [2]
      return dp.coinChange([...coins], 3) === -1
    })()
  )

  console.log(
    "6. 硬币找零边界情况测试:",
    (() => {
      const coins = [1, 2, 5]
      return dp.coinChange([...coins], 0) === 0
    })()
  )

  console.log(
    "7. 硬币找零大金额测试:",
    (() => {
      const coins = [3, 7, 405, 436]
      return dp.coinChange([...coins], 8839) === 25
    })()
  )

  console.log(
    "8. 最大子数组和标准情况测试:",
    (() => {
      const arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
      return dp.maxSubArray([...arr]) === 6
    })()
  )

  console.log(
    "9. 最大子数组和单元素测试:",
    (() => {
      return dp.maxSubArray([1]) === 1 && dp.maxSubArray([-1]) === -1
    })()
  )

  console.log(
    "10. 最大子数组和全正数测试:",
    (() => {
      const arr = [5, 4, -1, 7, 8]
      return dp.maxSubArray([...arr]) === 23
    })()
  )

  console.log(
    "11. 最大子数组和全负数测试:",
    (() => {
      const arr = [-2, -1]
      return dp.maxSubArray([...arr]) === -1
    })()
  )

  console.log(
    "12. 最大子数组和递增数组测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5]
      return dp.maxSubArray([...arr]) === 15
    })()
  )

  console.log(
    "13. 性能测试:",
    (() => {
      const start = performance.now()
      dp.fibonacci(30)
      const end = performance.now()
      return (end - start) < 1000 // 应该在1秒内完成
    })()
  )

  console.log(
    "14. this上下文优化测试:",
    (() => {
      const optObj = {
        multiplier: 2,
        processOptimization: function(weights, values, capacity) {
          const dp = new DynamicProgramming()
          const result = dp.knapsack(weights, values, capacity)
          return result * this.multiplier
        }
      }
      return optObj.processOptimization([1, 2], [3, 4], 3) === 14 // 7 * 2 = 14
    })()
  )

  console.log(
    "15. 复杂对象this绑定测试:",
    (() => {
      const optConfigObj = {
        config: { offset: 10 },
        processWithConfig: function(arr) {
          const dp = new DynamicProgramming()
          const result = dp.maxSubArray(arr)
          return result + this.config.offset
        }
      }
      return optConfigObj.processWithConfig([1, 2, 3]) === 16 // 6 + 10 = 16
    })()
  )
}

testOptimizationProblems()
